package com.org.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.org.config.Configurations;
import com.org.util.MD5Tools;


@Controller
@RequestMapping("/file")
public class FileUploadController {

	@RequestMapping(value = "uploadpic",method=RequestMethod.POST)
	public void fileUpload(@RequestParam MultipartFile[] myfiles,HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
		doOptions(request,response);
		
		JSONObject json = new JSONObject();
		String imgPath = Configurations.getFileRepository();
		File directory = new File(imgPath+File.separator+"picFile");
		if(!directory.exists()) {
			directory.mkdirs();
		}
		String desFileName = null;
		String fileNewName = null;
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		String originalFilename = null;
		for (MultipartFile myfile : myfiles) {
			if (myfile.isEmpty()) {
				json.put("msg","请选择文件后上传");
				out.write(json.toJSONString());
				out.flush();
			} else {
				originalFilename = myfile.getOriginalFilename();
				String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
				if (null != originalFilename && originalFilename.length() > 0) {
					fileNewName = UUID.randomUUID().toString().replaceAll("-", "") +"."+ suffix;
					desFileName = directory.toString() + "/" + fileNewName;
				}
				try {
					File file=new File(desFileName);
					FileUtils.copyInputStreamToFile(myfile.getInputStream(),
							file);
					String md5=MD5Tools.getFileMD5(file);
					//执行文件授权
					String command ="/usr/bin/chmod  644 "+file;
					String[] cmd = new String[]{"/bin/sh","-c",command};
					Runtime.getRuntime().exec(cmd);
					//执行文件授权
					json.put("md5",md5);
					System.out.println("md5"+md5);
 					json.put("msg", "上传成功");
 					json.put("imgurl", Configurations.getVisitURL()+"picFile/"+fileNewName);
 					json.put("fileName","picFile/"+fileNewName);
					out.write(json.toJSONString());
					out.flush();
				} catch (IOException e) {
					e.printStackTrace();
					json.put("msg","文件上传失败，请重试！！");
					out.write(json.toJSONString());
					out.flush();
				}
			}
		}
	}
	@RequestMapping(name="uploadpic",method=RequestMethod.OPTIONS)
	public void doOptions(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		resp.setHeader("Access-Control-Allow-Headers", "Content-Range,Content-Type");
		resp.setHeader("Access-Control-Allow-Origin", Configurations.getCrossOrigins());
		resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
	}
}
